/**
 * 
 */
package com.newworld.dmsapp2.fragment.adapter;

/**
 * @author zuo
 *
 */
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.fragment.bean.HouseManage;
import com.newworld.dmsapp2.util.Config;

/**
 * @author zuo 房屋管理列表界面的adapter
 * 
 */
public class HouseManageAdapter extends BaseAdapter {

	private ArrayList<HouseManage> houseList;
	private LayoutInflater inflater;
	Context context;

	public HouseManageAdapter(Context context,
			ArrayList<HouseManage> housemanageList) {
		inflater = LayoutInflater.from(context);
		this.houseList = housemanageList;
		this.context = context;

	}

	@Override
	public int getCount() {
		return houseList.size();
	}

	@Override
	public Object getItem(int position) {
		return houseList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_house_manage_list_item, parent, false);
			holder = new ViewHolder();
			holder.locationTv = (TextView) convertView
					.findViewById(R.id.tv_housemange_item_location);
			holder.relationshipTv = (TextView) convertView
					.findViewById(R.id.tv_housemange_item_relationship);
			holder.ifatatchTv = (TextView) convertView
					.findViewById(R.id.tv_housemange_item_ifatatch);
			holder.locationTv.setTag(position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.locationTv.setTag(position);
		}

		HouseManage item = houseList.get(position);
		holder.locationTv.setText(item.getProjectName() + item.getPhaseName()
				+ item.getBuildingName() + item.getUnitName()
				+ item.getHouseName());
		holder.relationshipTv.setText(item.getRelationType_typeName());

		if (item.getStatusType_typeCode().equals(
				Config.HOUSE_MANAGEMENT_ATATCHED)) {
//			holder.ifatatchTv.setText("已关联");
			holder.ifatatchTv.setVisibility(View.INVISIBLE);
//			holder.ifatatchTv.setTextColor(context.getResources().getColor(
//					R.color.nw_tx_white));
		} else {
			holder.ifatatchTv.setVisibility(View.VISIBLE);
			holder.ifatatchTv.setText(item.getStatusType_typeName());
			holder.ifatatchTv.setTextColor(context.getResources().getColor(
					R.color.nw_tx_red));

		}

		return convertView;
	}

	// 判断管理 房屋的状态，如果未处理不可以左滑，如果同意可以左滑解除关联
	@Override
	public int getItemViewType(int position) {
		// current menu type

		int ifagreenum = 0;
		if (houseList.get(position).getStatusType_typeCode()
				.equals(Config.HOUSE_MANAGEMENT_ATATCHED)) {
			ifagreenum = 0;
		} else if (houseList.get(position).getStatusType_typeCode()
				.equals(Config.HOUSE_MANAGEMENT_NOATATCHED)) {
			ifagreenum = 1;
		} else {
			ifagreenum = 2;
		}
		return ifagreenum;
	}

	@Override
	public int getViewTypeCount() {
		// menu type count
		return 2;
	}

	private class ViewHolder {

		TextView locationTv;

		TextView relationshipTv;
		TextView ifatatchTv;
	}
}